cmake_minimum_required(VERSION 3.10)
project(kaep_test C)

set(CMAKE_C_STANDARD 11)
set(CMAKE_C_STANDARD_REQUIRED ON)

if(CMAKE_BUILD_TYPE STREQUAL "Debug")
    set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -g -O0 -Wall -Wextra")
else()
    set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -O2 -Wall -Wextra")
endif()

if(ENABLE_ASAN)
    set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fsanitize=address -fno-omit-frame-pointer")
    set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -fsanitize=address")
endif()

set(KAEP_DEPENDENCY_DIRS
    ${CMAKE_SOURCE_DIR}/../platform/openhitls/build
    ${CMAKE_SOURCE_DIR}/../platform/openhitls/platform/Secure_C/lib
    ${CMAKE_SOURCE_DIR}/../build
    ${CMAKE_SOURCE_DIR}/platform/cmocka/build/src
    CACHE STRING "KAEP dependency library paths")

find_library(HITLS_CRYPTO_LIB hitls_crypto
    PATHS ${KAEP_DEPENDENCY_DIRS}
    NO_DEFAULT_PATH)
find_library(HITLS_BSL_LIB hitls_bsl
    PATHS ${KAEP_DEPENDENCY_DIRS}
    NO_DEFAULT_PATH)
find_library(HITLS_PKI_LIB hitls_pki
    PATHS ${KAEP_DEPENDENCY_DIRS}
    NO_DEFAULT_PATH)
find_library(HITLS_TLS_LIB hitls_tls
    PATHS ${KAEP_DEPENDENCY_DIRS}
    NO_DEFAULT_PATH)
find_library(SECUREC_LIB boundscheck
    PATHS ${KAEP_DEPENDENCY_DIRS}
    NO_DEFAULT_PATH)
find_library(KAEP_PROVIDER_LIB kae_provider
    PATHS ${KAEP_DEPENDENCY_DIRS}
    NO_DEFAULT_PATH)
find_library(CMOCKA_LIB cmocka
    PATHS ${KAEP_DEPENDENCY_DIRS}
    NO_DEFAULT_PATH)

foreach(LIB IN ITEMS
    HITLS_CRYPTO_LIB
    HITLS_BSL_LIB
    HITLS_PKI_LIB
    HITLS_TLS_LIB
    SECUREC_LIB
    KAEP_PROVIDER_LIB
    CMOCKA_LIB)
    if(NOT ${LIB})
        message(FATAL_ERROR "Required library ${LIB} not found in: ${PQCP_DEPENDENCY_DIRS}")
    endif()
endforeach()

if(CUSTOM_CFLAGS)
    message(STATUS "CUSTOM_CFLAGS: ${CUSTOM_CFLAGS}")
    set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${CUSTOM_CFLAGS}")
endif()

include_directories(
    ${CMAKE_SOURCE_DIR}/common
    ${CMAKE_SOURCE_DIR}/../include

    ${CMAKE_SOURCE_DIR}/../src/include

    ${CMAKE_SOURCE_DIR}/../src/kaev1/alg/cipher
    ${CMAKE_SOURCE_DIR}/../src/kaev1/alg/digests
    ${CMAKE_SOURCE_DIR}/../src/kaev1/alg/dh/include
    ${CMAKE_SOURCE_DIR}/../src/kaev1/alg/rsa/include
    ${CMAKE_SOURCE_DIR}/../src/kaev1/util
    ${CMAKE_CURRENT_SOURCE_DIR}/../src/kaev1/provider
    ${CMAKE_CURRENT_SOURCE_DIR}/../src/kaev1/codecskey/include

    ${CMAKE_SOURCE_DIR}/../platform/openhitls/include/crypto
    ${CMAKE_SOURCE_DIR}/../platform/openhitls/include/bsl
    ${CMAKE_SOURCE_DIR}/../platform/openhitls/include/pki
    ${CMAKE_SOURCE_DIR}/../platform/openhitls/include/tls
    ${CMAKE_SOURCE_DIR}/../platform/openhitls/bsl/include
    ${CMAKE_SOURCE_DIR}/../platform/openhitls/bsl/err/include
    ${CMAKE_SOURCE_DIR}/../platform/openhitls/bsl/hash/include
    ${CMAKE_SOURCE_DIR}/../platform/openhitls/bsl/sal/include
    ${CMAKE_SOURCE_DIR}/../platform/openhitls/config/macro_config
    ${CMAKE_SOURCE_DIR}/../platform/openhitls/crypto/include
    ${CMAKE_SOURCE_DIR}/../platform/openhitls/crypto/aes/include
    ${CMAKE_SOURCE_DIR}/../platform/openhitls/crypto/bn/include
    ${CMAKE_SOURCE_DIR}/../platform/openhitls/crypto/dh/include
    ${CMAKE_SOURCE_DIR}/../platform/openhitls/crypto/eal/src
    ${CMAKE_SOURCE_DIR}/../platform/openhitls/crypto/rsa/include
    ${CMAKE_SOURCE_DIR}/../platform/openhitls/crypto/md5/include
    ${CMAKE_SOURCE_DIR}/../platform/openhitls/crypto/sha1/include
    ${CMAKE_SOURCE_DIR}/../platform/openhitls/crypto/sm3/include
    ${CMAKE_SOURCE_DIR}/../platform/openhitls/crypto/sm4/include
    ${CMAKE_SOURCE_DIR}/../platform/openhitls/crypto/provider/include
    ${CMAKE_SOURCE_DIR}/../platform/openhitls/platform/Secure_C/include
    ${CMAKE_SOURCE_DIR}/../platform/openhitls/tls/include
    ${CMAKE_SOURCE_DIR}/../platform/openhitls/tls/crypt/include
    ${CMAKE_SOURCE_DIR}/../platform/openhitls/tls/cert/include
    ${CMAKE_SOURCE_DIR}/../platform/openhitls/tls/handshake/include
    ${CMAKE_SOURCE_DIR}/../platform/openhitls/tls/handshake/common/include
    ${CMAKE_SOURCE_DIR}/../platform/openhitls/tls/record/include
    ${CMAKE_SOURCE_DIR}/../platform/openhitls/tls/feature/custom_extensions/include
    ${CMAKE_SOURCE_DIR}/platform/cmocka/include
    ${CMAKE_SOURCE_DIR}/helper
    ${WD_DIR}
    ${WD_DIR}/include
)

file(GLOB_RECURSE SDV_SOURCES
    "sdv/aes/test_aes.c"
    "sdv/dh/test_dh.c"
    "sdv/md5/test_md5.c"
    "sdv/rsa/test_rsa_api.c"
    "sdv/rsa/test_rsa_enc.c"
    "sdv/rsa/test_rsa_sign.c"
    "sdv/sha1/test_sha1.c"
    "sdv/sha2/test_sha2.c"
    "sdv/sm3/test_sm3.c"
    "sdv/sm4/test_sm4.c"
    "sdv/tls/test_provider_group.c"
    "sdv/tls/test_provider_sign.c"
)

set(SDV_TEST_TARGETS)

if(BUILD_SDV)
    foreach(test_source ${SDV_SOURCES})
        get_filename_component(test_name ${test_source} NAME_WE)
        add_executable(sdv_${test_name} ${test_source})
        target_link_libraries(sdv_${test_name}
            ${HITLS_CRYPTO_LIB}
            ${HITLS_BSL_LIB}
            ${HITLS_PKI_LIB}
            ${HITLS_TLS_LIB}
            ${KAEP_PROVIDER_LIB}
            ${SECUREC_LIB}
            ${CMOCKA_LIB}
            wd
            pthread
        )
        list(APPEND SDV_TEST_TARGETS sdv_${test_name})
    endforeach()
endif()

add_library(tls_provider SHARED "provider/tls_provider.c")
target_link_libraries(tls_provider
        ${HITLS_CRYPTO_LIB}
        ${HITLS_BSL_LIB}
        ${HITLS_PKI_LIB}
        ${HITLS_TLS_LIB}
        ${KAEP_PROVIDER_LIB}
        ${SECUREC_LIB}
        wd
)